﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ETL.Common
{
    public class Utils
    {
        /// <summary>
        /// Cast a nullable short number to int
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public static int NullableShortToInt(short? number)
        {
            int i = 0;
            if (number != null)
            {
                try
                {
                    i = int.Parse(number.ToString());
                }
                catch (Exception ex)
                {
                    return 0;
                }
            }
            return i;
        }

        /// <summary>
        /// Check whether a string is numeric or not
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsNumeric(string s)
        {
            bool result = true;
            foreach (char c in s)
            {
                if(!char.IsDigit(c))
                {
                    result = false;
                    break;
                }
            }
            return result;
        }

        public static bool IsValidDateTime(string date, string cultureInfo)
        {
            try
            {
                DateTime.Parse(date, System.Globalization.CultureInfo.GetCultureInfo(cultureInfo));
                return true;
            }
            catch (Exception)
            {
                return false;
                throw;
            }
        }
        
        public static DateTime ConvertToDate(string date, string cultureInfo)
        {
            try
            {
               var result = DateTime.Parse(date, System.Globalization.CultureInfo.GetCultureInfo(cultureInfo));
                return result;
            }
            catch (Exception)
            {
                return DateTime.Now;
                throw;
            }
        }

        public static decimal  StringToDecimal(string s)
        {
            decimal defaultValue = 0;
            decimal.TryParse(s, out defaultValue);
            return defaultValue;
        }
    }
}
